<?php
/**
 * 首页友情链接类
 * author: heimuya
 * time: 2014-4-15
 */
 namespace Admin\Controller;
 use Think\Controller;
 
class LinkController extends CommonController {
	
	public function _initialize(){
		parent::_initialize();
	}
	
	public function link(){
		$LINK = M('Link');
		$count = $LINK->count();
		$Page = new \Think\Page($count, 10);
		$list = $LINK->limit($Page->firstRow. ',' . $Page->listRows)->order('ctime desc')->select();
		$this->empty = "<td colspan='7' class='tb_heade'>暂无数据</td>";
		
		$page = $Page->show();

        $this->assign("page", $page);
		$this->assign('list',$list);
		$this->display();
	}
	
	public function addlink(){
		//C('TOKEN_ON',false);
		$this->display();
	}
	
	public function doadd(){
		$data['sequence'] = $this->checkSequence('Link', I('post.sequence'));
		$data['name'] = I('post.name');
		$data['url'] = I('post.url');
		$data['visible'] = I('post.ifshow');
		$data['ctime'] = time();
		$LINK = D('Link');
		if($LINK->add($data)){
			$this->success('增加成功');
		}else{
			$this->error('增加失败');
		}
	}
	
	public function editlink(){
		$id = I('id');
		if($id == ''){
			$this->error('参数错误');
		}
		$LK = M('Link');
		$this->edit = $LK->where(array('id'=>$id))->find();
		$this->display();
	}
	
	public function doedit(){
		$id = I('id','','int');
		if($id == ''){
			$this->error('参数错误');
		}
		$data['sequence'] = $this->checkSequence('Link', I('post.sequence'), 'edit', $id);
		$LK = M('Link');
		if(I('name') != ''){
			$data['name'] = I('name');
		}
		if(I('url') != ''){
			$data['url'] = I('url');
		}
		$data['visible'] = I('ifshow');
		if($rs = $LK->where(array('id'=>$id))->setField($data)){
			$this->success('操作成功', U('Link/link'));
		}else {
			$this->error('操作失败');
		}
	}
	
	public function dellink(){
		$id = I('id');
		if($id == ''){
			$this->error('参数错误');
		}
		if($rs = M('Link')->where(array('id'=>$id))->delete()){
			$this->success('删除成功');
		}else {
			$this->error('删除失败');
		}
	}
}